package com.heima.wemedia.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.model.admin.dtos.SenstitiveDto;
import com.heima.model.admin.pojos.AdSensitive;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.wemedia.pojos.WmSensitive;
import com.heima.wemedia.mapper.WmSensitiveMapper;
import com.heima.wemedia.service.WmSensitveService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.Date;


@Slf4j
@Service
public class WmSensitveServiceImpl extends ServiceImpl<WmSensitiveMapper, WmSensitive> implements WmSensitveService {


    /**
     * 分页查询敏感词列表
     *
     * @param dto
     * @return
     */
    @Override
    public ResponseResult findList(SenstitiveDto dto) {
        //1 检查参数
        if (dto == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        //2 进行分页
        IPage page = new Page(dto.getPage(),dto.getSize());

        //3 按照敏感词名称进行模糊查询（可选）
        LambdaQueryWrapper<WmSensitive> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        if(StringUtils.isNotBlank(dto.getName())){
            lambdaQueryWrapper.like(WmSensitive::getSensitives,dto.getName());
        }

        //4 按照发布时间倒叙排序
        lambdaQueryWrapper.orderByDesc(WmSensitive::getCreatedTime);

        page = page(page,lambdaQueryWrapper);

        //5 结果封装
        ResponseResult responseResult = new PageResponseResult(dto.getPage(),dto.getSize(), (int) page.getTotal());
        responseResult.setData(page.getRecords());
        return responseResult;
    }

    /**
     * 保存敏感词
     *
     * @param adSensitive
     * @return
     */
    @Override
    public ResponseResult saveSensitive(AdSensitive adSensitive) {
        //1 检查敏感词
        if (adSensitive == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        //2 检查敏感词是否存在
        WmSensitive sensitive = getOne(Wrappers.<WmSensitive>lambdaQuery().eq(WmSensitive::getSensitives, adSensitive.getSensitives()));
        if (sensitive != null){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST);
        }


        //3 新增敏感词
        sensitive = new WmSensitive();
        BeanUtils.copyProperties(adSensitive,sensitive);
        sensitive.setCreatedTime(new Date());
        save(sensitive);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    /**
     * 修改敏感词
     *
     * @param adSensitive
     * @return
     */
    @Override
    public ResponseResult updateSentive(AdSensitive adSensitive) {
        //1 检查参数
        if(adSensitive == null || adSensitive.getId() == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        //2 修改数据
        WmSensitive wmSensitive = new WmSensitive();
        BeanUtils.copyProperties(adSensitive,wmSensitive);
        updateById(wmSensitive);

        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    /**
     * 删除敏感词
     *
     * @param id
     * @return
     */
    @Override
    public ResponseResult delSensitive(Integer id) {
        //1 检查参数
        if (id == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        //2 删除参数
        removeById(id);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }
}
